package com.jeespring.pay.controller;

import com.alibaba.fastjson.JSON;
import com.jeespring.dto.String;
import com.jeespring.entity.Pay;
import com.jeespring.pay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private int port;

    @PostMapping("/insert")
    public String insert(@RequestBody Pay pay){
        String<Pay> String = new String<>();
        pay.setMoney(100L);
        pay.setOrderId(1L);
        payService.insert(pay);
        String.setData(pay);
        String.setMsg("请求成功，请求端口号：" + port);
        return String;
    }

    @GetMapping("/get/{id}")
    public String selectById(@PathVariable Long id){
        String<Pay> String = new String<>();
        Pay pay = payService.selectById(id);
        String.setData(pay);
        String.setMsg("请求成功，请求端口号：" + port);
        return String;
    }

    @GetMapping("/discovery")
    public Object discovery(){
        String String = new String();
        List<String> services = discoveryClient.getServices();
        System.out.println(JSON.toJSONString(services));
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRING-CLOUD-PAY");
        System.out.println(JSON.toJSONString(instances));
        String.setData(discoveryClient);
        return String;
    }

    @GetMapping("/timeout")
    public String timeout() throws Exception{
        String<String> String = new String<>();
        String.setData("超时调用");
        Thread.sleep(3000);
        String.setMsg("请求成功，请求端口号：" + port);
        return String;
    }
}
